/* $Id: region.js 2807 2006-11-21 02:58:47Z scottye $ */

var region = new Object();

region.filename = location.href.substring((location.href.lastIndexOf("/")) + 1, location.href.lastIndexOf("."));
region.selCountries = 'selCountries';
region.selProvinces = 'selProvinces';
region.selCities    = 'selCities';
region.selDistricts = 'selDistricts';
region.isAdmin      = true;

/**
 * 载入指定的国家下所有的省份
 *
 * @country integer     国家的编号
 * @selName string      列表框的名称
 */
region.loadProvinces = function(country, selName)
{
    if (region.isAdmin)
    {
        listZone.showLoader();
    }
    else
    {
        toggleLoader(true);
    }
    
    var objName = (typeof selName == "undefined") ? "selProvinces" : selName;

    cp.set_response_type("object");
    cp.call(region.getFileName(), 'region_result', region.response, country, objName, 1);
}

/**
 * 载入指定的省份下所有的城市
 *
 * @province    integer 省份的编号
 * @selName     string  列表框的名称
 */
region.loadCities = function(province, selName)
{
    if (region.isAdmin)
    {
        listZone.showLoader();
    }
    else
    {
        toggleLoader(true);
    }

    var objName = (typeof selName == "undefined") ? "selCities" : selName;

    cp.set_response_type("object");
    cp.call(region.getFileName(), 'region_result', region.response, province, objName, 2);
}


/**
 * 载入指定的城市下的区/县
 *
 * @city    integer     城市的编号
 * @selName string      列表框的名称
 */
region.loadDistricts = function(city, selName)
{
    var objName = (typeof selName == "undefined") ? "selDistricts" : selName;

    if (region.isAdmin)
    {
        listZone.showLoader();
    }
    else
    {
        toggleLoader(true);
    }

    cp.set_response_type("object");
    cp.call(region.getFileName(), 'region_result', region.response, province, objName, 3);
}


/**
 * 处理下拉列表改变的函数
 *
 * @obj     object  下拉列表
 * @type    integer 类型
 * @selName string  目标列表框的名称
 */
region.changed = function(obj, type, selName)
{
    if (region.isAdmin)
    {
        listZone.showLoader();
    }
    else
    {
        toggleLoader(true);
    }

    var parent = obj.options[obj.selectedIndex].value;

    cp.set_response_type("object");
    cp.call(region.getFileName(), 'region_result', region.response, parent, selName, type);


    switch (type)
    {
        case 0:
            document.getElementById(region.selProvinces).length = 1;
            document.getElementById(region.selCities).length = 1;
            if (document.getElementById(region.selDistricts)) document.getElementById(region.selDistricts).length = 1;
            break;
        case 1:
            document.getElementById(region.selCities).length = 1;
            if (document.getElementById(region.selDistricts)) document.getElementById(region.selDistricts).length = 1;
            break;
        case 2:
            if (document.getElementById(region.selDistricts)) document.getElementById(region.selDistricts).length = 1;
            break;
    }
}

region.response = function(result, text_result)
{
    if (region.isAdmin)
    {
        listZone.hideLoader();
    }
    else
    {
        toggleLoader(false);
    }

    var sel = document.getElementById(result.ajaxResponse[0].select[0].data);
    sel.length = 1;

    if (!region.isAdmin)
    {
        if (!result.ajaxResponse[0].region && sel.name.toLowerCase().indexOf('district') > -1)
        {
            sel.style.display="none";
            return;
        }
        else
        {
            sel.style.display="";
        }
    }

    if (result.ajaxResponse[0].region != undefined)
    {
        var count = result.ajaxResponse[0].region.length;
        for (i = 0; i < count; i++)
        {
            opt = document.createElement("OPTION");
            opt.value   = result.ajaxResponse[0].region[i].id[0].data;
            opt.text    = result.ajaxResponse[0].region[i].name[0].data;

            sel.options.add(opt);
        }
    }
}

region.getFileName = function()
{
    if (region.isAdmin)
    {
        return region.filename + ".php?act=ajax";
    }
    else
    {
        return "ajax.php";
    }
}